<!DOCTYPE html>
<html>
<title>菜单管理</title>
<head>
    <link href="/css/layui/css/layui.css" rel="stylesheet">
    <link href="/css/hadmin/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/bootstrap-datetimepicker.min.css" rel="stylesheet">
    <link href="/css/hadmin/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/hadmin/css/animate.css" rel="stylesheet">
    <link href="/css/hadmin/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="/css/hadmin/css/animate.css" rel="stylesheet">
    <link href="/css/bootstrap-select.min.css" rel="stylesheet">
    <link href="/css/multi-select.dist.css" rel="stylesheet">
</head>

<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h5>角色管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="form-group">
                        <form class="form-inline" method="post" action="/role/index">
                            <input type="text" name="role_name" value="<?=$role_name?>" placeholder="请输入角色名" class="form-control">
                            <button type="submit" class="btn btn-primary" id="search"> 搜索角色</button>
                            <a class="btn btn-primary radius" data-toggle="modal"
                               data-target="#myModal">添加角色</a>
                        </form>
                    </div>
                </div>
                <div class="page-container">
                    <div class="mt-20">
                        <table class="table table-hover table-bordered datatable">
                            <colgroup>
                                <col width="150">
                                <col width="200">
                                <col>
                            </colgroup>
                            <tr>
                                <th>编号</th>
                                <th>角色名</th>
                                <th>管理员列表</th>
                                <th>描述</th>
                                <th>操作</th>
                            </tr>
                            <?php foreach($params as $v): ?>
                            <tr>
                                <td><?=$v['Role']['id']?></td>
                                <td><?=$v['Role']['role_name']?></td>
                                <td><?=$v['Role']['name']?></td>
                                <td><?=$v['Role']['desc']?></td>
                                <td>
                                    <?php if($v['Role']['id'] == 1): ?>
                                    <?php else: ?>
                                    <a class="layui-icon layui-icon-edit" data-toggle="modal" data-target="#myModal"
                                       onclick="edit(<?= $v['Role']['id']?>,'<?= $v['Role']['role_name']?>','<?= $v['Role']['desc']?>')"></a>
                                    <a class="glyphicon glyphicon-align-justify" href="\role\grant_auth?id=<?= $v['Role']['id']?>">

                                    </a>
                                    <a class="layui-icon layui-icon-subtraction"
                                       onclick="del(<?= $v['Role']['id']?>)"></a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" data-backdrop="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" onclick="close1()" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">角色操作</h4>
            </div>
            <div class="modal-body">
                <div class="form">
                    <div class="form-group">
                        <label class="control-label">角色名</label>
                        <input type="hidden" id="id">
                        <input type="text" id="username" placeholder="请输入角色名" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="control-label">角色描述</label>
                        <input type="text" id="desc" placeholder="请输入描述" class="form-control">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="close1()" data-dismiss="modal">返回</button>
                <button type="button" id="ajax_submit1" class="btn btn-primary">提交</button>
            </div>
        </div>
    </div>
</div>

<script src="/css/hadmin/js/jquery.min.js?v=2.1.4"></script>
<script src="/css/hadmin/js/bootstrap.min.js?v=3.3.6"></script>
<script src="/css/hadmin/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="/css/hadmin/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="/css/hadmin/js/plugins/layer/layer.min.js"></script>
<script src="/css/hadmin/js/index.js"></script>
<script src="/css/hadmin/js/plugins/toastr/toastr.min.js"></script>
<script src="/js/jquery.fancybox.min.js"></script>
<script src="/js/cupload.js"></script>
<script src="/js/comment.js"></script>

<script>
    //关闭模态框重新加载
    function close1() {
        window.location.reload();
    }

     //提交修改
    $("#ajax_submit1").click(function () {
            //数据
            var data = {
                'id': $('#id').val(),
                'role_name': $('#username').val(),
                'desc': $('#desc').val(),
            };
            $.ajax({
                'url': '/Role/dellRole',
                'data': data,
                'type': 'post',
                'dataType': 'json',
                'success': function (res) {
                    if (res.code == 400) {
                        layer.msg("操作失败!");
                    } else if (res.code == 200) {
                        layer.msg("操作成功!");
                    }
                    setTimeout(function () {
                        window.location.reload();
                    }, 800)
                }
            });
        });

    //点击修改时加载
    function edit(role_id, role_name, desc) {
        $("#username").val(role_name);
        $("#desc").val(desc);
        $('#id').val(role_id);
    }

    //删除角色信息
    function del(role_id) {
        var data = {
            'role_id': role_id
        };
        $.ajax({
            'url': '/Role/delRole',
            'type': 'post',
            'dataType': 'Json',
            'data': data,
            'success': function (res) {
                if (res.code == 200) {
                    success("操作成功!");
                } else {
                    error("操作失败!");
                }
                setTimeout(function () {
                    window.location.reload();
                }, 800);
            }
        });
    }
</script>